function [Fx, Fy] = comb_vfields(VBas, alpha)
%COMB_VFIELDS Linear Combination of Vector fields
%
%   [Fx, Fy] = COMB_VFIELDS(VBas, alpha);
%
%       Generates a velocity field from a set of basis fields given by
%       VBas, through a linear combination with coefficient vector a.
%
%       Let K be the number of bases in VBas. alpha should be a matrix of
%       size K x m (m is the number of resultant fields).
%
%       Then Fx and Fy are arrays of size h x w x m, where h and w are
%       respectively the height and width of the field. They respectively
%       represent the x and y components of the fields.
%

% Created by Dahua Lin, on April 5, 2012

%% verify arguments

if ~is_vbasis(VBas)
    error('comb_vfields:invalidarg', ...
        'VBas should be a set of vector field basis.');
end

K = VBas.K;

if ~(isfloat(alpha) && isreal(alpha) && ismatrix(alpha) && size(alpha,1) == K)
    error('comb_vfields:invalidarg', ...
        'alpha should be a real matrix of K rows.');
end

m = size(alpha, 2);

%% main

Fx = VBas.Bx * alpha;
Fy = VBas.By * alpha;

if m == 1
    Fx = reshape(Fx, VBas.size);
    Fy = reshape(Fy, VBas.size);
else
    Fx = reshape(Fx, [VBas.size, m]);
    Fy = reshape(Fy, [VBas.size, m]);
end

